import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-02-07
 * Time: 7:33
 */
class Solution2 {
    List<List<Integer>> ret;
    HashSet<List<Integer>> set;
    List<Integer> path;
    boolean[] check;
    public List<List<Integer>> permuteUnique(int[] nums) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        check = new boolean[nums.length];
        set = new HashSet<>();
        dfs(nums);

        for(List<Integer> list : set){
            ret.add(list);
        }
        return ret;

    }
    public void dfs(int[] nums){
        if(nums.length == path.size()){
            set.add(new ArrayList<>(path));
            return;
        }
        for(int i = 0 ; i < nums.length ; i++){
            if(check[i] == false){
                path.add(nums[i]);
                check[i] = true;
                dfs(nums);
                //回溯剪枝
                path.remove(path.size() - 1);
                check[i] = false;
            }
        }
    }
}
public class Test2 {
}
